package com.csu.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.csu.common.CommonResponse;
import com.csu.entity.News;
import com.csu.entity.StudentEvent;
import com.csu.service.NewsService;
import com.csu.service.StudentEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class NewsController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private StudentEventService studentEventService;
    /**
     * 获取新闻列表
     */
    @GetMapping("/news")
    //@SaIgnore
    public CommonResponse<List<News>> getNewsList(){
        System.out.println("news;request");
        List<News> news = newsService.getNewsList();
        if (news != null)
            return CommonResponse.createForSuccess("SUCCESS",news);
        else
            return CommonResponse.createForError("新闻列表为空！");
    }

    /**
     * 分组获取新闻列表中的新闻
     */
    @GetMapping("/newsByCategory")
    //@SaIgnore
    public CommonResponse<List<News>> getNewsListGroupByNew(@RequestParam("category") int category){
        List<News> news = newsService.getNewsListByNew(category);
        if (news != null)
            return CommonResponse.createForSuccess("SUCCESS",news);
        else
            return CommonResponse.createForError("查不到新闻列表！");
    }
    /**
     * 获取学生活动列表
     */
    @GetMapping("/studentevents/{userId}")
    //@SaCheckRole("parent")
    public CommonResponse<List<StudentEvent>> getStudentEventList(@PathVariable("userId") int userId){
        List<StudentEvent> studentEvents = studentEventService.getAllStudentEvent(userId);
        if (studentEvents != null)
            return CommonResponse.createForSuccess("SUCCESS",studentEvents);
        else
            return CommonResponse.createForError("获取不到学生活动列表！");
    }
}
